package org.goyo.main.services.sale.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.goyo.common.exception.DBException;
import org.goyo.common.exception.ServiceException;
import org.goyo.main.domain.dao.sale.IDispatchDao;
import org.goyo.main.domain.dao.sale.ILossDao;
import org.goyo.main.domain.dao.sale.ISaleOrderDao;
import org.goyo.main.domain.entity.sale.Loss;
import org.goyo.main.services.BaseService;
import org.goyo.main.services.sale.ILossService;
import org.springframework.stereotype.Service;

@Service
@Transactional
public class LossServiceImpl extends BaseService  implements  ILossService{
	@Resource
	private ILossDao lossDao;
	@Resource
	private ISaleOrderDao saleOrderDao;
	@Resource
	private IDispatchDao dispatchDao;

	@Override
	public BigDecimal saveOrUpdateInfo(List<Loss> gridList)	throws DBException, ServiceException {
		BigDecimal amount = new BigDecimal(0);
		for(Loss loss : gridList){
			String uuid = loss.getUuid();
			if(uuid == null || "".equals(uuid)){
				loss.setUuid(null);
				uuid = lossDao.save(loss);
			}else{
				lossDao.update(loss);
			}
			dispatchDao.updateByUuidLoss(loss.getResourceEntryId(), loss.getLossQty(), loss.getLossAmount());
		}
		return amount;
	}

	@Override
	public List<Loss> findByResourceId(String parentId) throws DBException,
			ServiceException {
		return lossDao.findByResourceId(parentId);
	}

}
